import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

//createRouter 创建路由实例
//1.createWebRouter history模式 地址栏不带#
//2.createHashRouter hash模式 地址栏带#
//vite中的环境变量import.meta.env.BASE_URL,就是vite.config.js中的base配置项（vite官方文档可见）
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') }, //登录页
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue/'),
      redirect: '/article/manage',
      children: [
        {
          path: '/article/manage',
          component: () => import('@/views/article/ArticleManage.vue')
        },
        {
          path: '/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        },
        {
          path: '/article/channel',
          component: () => import('@/views/article/ArticleChannel.vue')
        }
      ]
    }
  ]
})

//登录拦截
router.beforeEach((to) => {
  //没有token，且访问的是登录页，拦截；其他情况放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
})
export default router
